package cn.acac.yygh.order.controller;

import cn.acac.yygh.enums.OrderStatusEnum;
import cn.acac.yygh.enums.PaymentStatusEnum;
import cn.acac.yygh.model.order.OrderInfo;
import cn.acac.yygh.model.order.PaymentInfo;
import cn.acac.yygh.order.service.OrderInfoService;
import cn.acac.yygh.order.service.PaymentService;
import cn.acac.yygh.order.service.WeixinService;
import cn.acac.yygh.result.R;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * 微信支付控制器
 */
@RestController
@Api("微信支付的控制器")
@RequestMapping("/user/order/weixin")
public class WeixinController {

    @Autowired
    private WeixinService weixinService;

    @Autowired
    private OrderInfoService orderInfoService;

    @Autowired
    private PaymentService paymentService;

    /**
     * 根据订单id生成微信支付的二维码
     * @param orderId
     * @return
     */
    @GetMapping("/createNative/{orderId}")
    public R createNative(@PathVariable Long orderId){
        String url = weixinService.createNative(orderId);
        return R.ok().setData("url",url);
    }


    /**
     * 获取订单状态
     * @param orderId
     * @return
     */
    @GetMapping("/status/{orderId}")
    public R getPayStatus(@PathVariable Long orderId){
        Map<String,String> map = weixinService.getPayStatus(orderId);
        // 进行判断
        if(map == null){
            return R.error().message("查询失败！");
        }
        // 支付成功判断
        if(map.get("trade_state").equals("SUCCESS")){
            weixinService.paySuccess(orderId,map);
            return R.ok().message("支付成功！");
        }
        return R.ok().message("支付中");
    }


}
